package com.zhs.gccs.http;

import android.content.Context;

import androidx.fragment.app.FragmentActivity;

import com.orhanobut.logger.Logger;
import com.zhs.gccs.R;
import com.zhs.gccs.dialog.SimpleLoadDialog;
import com.zhs.gccs.utils.NetWorkUtils;
import com.zhs.gccs.utils.ToastUtils;

import java.net.ConnectException;
import java.net.SocketTimeoutException;

import io.reactivex.Observer;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import retrofit2.HttpException;


public abstract class BackgroudObserver implements Observer<String>, ProgressCancelListener {
    Context context;
    public static final String TAG = BackgroudObserver.class.getSimpleName();
    public BackgroudObserver(Context context) {
        this.context = context;
    }

    @Override
    public void onSubscribe(@NonNull Disposable d) {
//        if (!NetWorkUtils.isOnline(context)) {
//        }
    }

    @Override
    public void onNext(@NonNull String result) {
//        dismissProgressDialog();
        _onNext(result);
    }

    @Override
    public void onError(@NonNull Throwable e) {
        _onError(e.getMessage());
        if (e instanceof SocketTimeoutException) {
//            ToastUtils.toast(activity.getString(R.string.tip_connect_timeout_error));
        } else if (e instanceof ConnectException) {
//            ToastUtils.toast(activity.getString(R.string.tip_connect_error));
        } else if (e instanceof retrofit2.adapter.rxjava2.HttpException || e instanceof HttpException) {
            int code = ((HttpException) e).code();
            if (code == 404) {
                ToastUtils.toast("网络繁忙，请稍候再试");
            } else if (code == 500) {
                ToastUtils.toast("页面失联了，攻城狮正在紧急搜索中…");
            }
        }
    }

    @Override
    public void onComplete() {
    }

    @Override
    public void onCancelProgress() {
    }

    protected abstract void _onNext(String result);

    protected abstract void _onError(String result);

}
